<?php
/**
 * 设计模式 -- 观察者模式
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-3-24
 * Time: 下午4:29
 */

class subject{
    private $arr_observe = array();

    //增加观察者
    public function attach($observe){
        $this->arr_observe[] = $observe;
    }

    //输出当前时间
    public function get_time(){
        echo 'subject 有变动 -->';
        echo date('Y-m-d H:i:s'),'<br/>';
    }

    public function notify(){
        foreach($this->arr_observe as $val){
            $val->update($this);
        }
    }
}


/**
 * 观察者1
 * Class observe1
 */
class observe1{
    public function update($subject){
        $subject->get_time();
    }
}

/**
 * 观察者2
 * Class observe2
 */
class observe2{
    public function update($subject){
        $subject->get_time();
    }
}

header('Content-Type: text/html;charset=utf-8');

$subject = new subject();

$observe1 = new observe1();
$observe2 = new observe2();
$subject->attach($observe1);
$subject->attach($observe2);
//通知观察者
$subject->notify();









